<template>
  <el-container class="home-container">
    <el-header>
      <div class="title">
        <img class="icon" src="./../assets/bg011.png" alt="">
        <span>点餐系统</span>
      </div>
      <div class="flex-center">
        <span class="mr-10 font15">{{'欢迎你 '+getUserLevelName+' '+userInfo.userName+'（'+userInfo.score+'元）'}}</span>
        <el-button type="primary" @click="logout">退出</el-button>
      </div>
    </el-header>

    <el-container>
      <el-aside :width="isCollapse ? '64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu background-color="#333744" unique-opened text-color="#fff" active-text-color="#409EFF"
                 :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
          <el-submenu :index="item.name" v-for="(item,index) in menus" :key="index">
            <template slot="title">
              <span>{{ item.name }}</span>
            </template>
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState( subItem.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.name }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
    
  </el-container>
</template>

<script>
// @ is an alias to /src

import {getStore, setStore} from "js-utils";
import {Validate} from "js-utils/validate";
import {mapState} from "vuex";
import {getUserInfo} from "@/config/tools";

export default {
  name: 'HomeView',
  components: {
  },
  computed:{
    getUserLevelName(){
      if (Validate.isEmpty(this.userInfo)){
        return '';
      }
      if (this.userInfo.level === 1){
        return '超级管理员'
      }else if(this.userInfo.level ===2){
        return '管理员'
      }else if(this.userInfo.level ===3){
        return '注册用户'
      }else if(this.userInfo.level ===4){
        return '厨师'
      }
      return ''
    },
    userInfo(){
      return this.$store.state.userInfo||{};
    }
  },
  data(){
    return {
      menus :[
          {name:'菜品管理',children:[{name:'菜品列表',path:'/orderingIndex'}]},
      ],
      isCollapse:true,
      activePath:''
    }
  },
  created (){
    this.activePath = getStore('activePath');
    this.getInfo();
  },
  methods:{
    getInfo(){
      getUserInfo().then(()=>{
        console.log(this.userInfo)
        if (!Validate.isEmpty(this.userInfo)){
          if(this.userInfo.level < 3){
            let dic = this.menus[0]
            let childrenArr = dic['children']
            childrenArr.push({name:'档位管理',path:'/gearsManage'})
            childrenArr.push({name:'菜品发布',path:'/orderPost'})
            dic['childrenArr'] = childrenArr
            this.menus.push( {name:'订单管理',children:[{name:'菜品订单',path:'/orderList'},{name:'消费订单',path:'/scoreList'}]})
            this.menus.push( {name:'用户管理',children:[{name:'用户管理',path:'/userIndex'},{name:'用户面板',path:'/userPanel'}]})
          }else if(this.userInfo.level === 3){
            let dic = this.menus[0]
            dic['name'] = '操作面板';
            (dic['children']).push({name:'用户面板',path:'/userPanel'});
            this.menus.push( {name:'订单管理',children:[{name:'菜品订单',path:'/orderList'},{name:'消费订单',path:'/scoreList'}]})
          }else {
            let dic = this.menus[0]
            dic['name'] = '操作面板';
            (dic['children']) = [{name:'用餐管理',path:'/mealManager'}];
          }
        }else{
          this.logout()
        }
      }).catch((err)=>{
        console.log(err)
        // this.logout()
      })
    },
    logout(){
      setStore('token',null,true)
      setTimeout(()=>{
        this.$router.go(0)
      },300)
    },
    toggleCollapse(){
      this.isCollapse = !this.isCollapse

    },
    // 保存选中链接的值
    saveNavState(path){
      setStore('activePath',path)
      this.$router.push(path)
    }
  }
}
</script>
<style lang='scss'>
  .icon{
    width: 50px;
    height: 50px;
  }
  .toggle-button{
    background-color: #4A5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;//鼠标放上去就有个小手
  }
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: white;
    font-size: 20px;
    
  }
  .title{
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }   
  }
  .el-aside {
    background-color: #333744;
    .el-menu{
      border-right: 0;
    }
  }
  
  .el-main {
    background-color: #EAEDF1;
  }
  .home-container{
    height: 100%;
  }

</style>